package cn.edu.hstc.wm.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.ksoap2.serialization.SoapObject;

import android.util.Log;

import cn.edu.hstc.wm.bean.Comment;
import cn.edu.hstc.wm.bean.Good;
import cn.edu.hstc.wm.bean.Store;

/**
 * @ClassName: CommentService
 * @Description: 操作评论的服务程序接口
 * 
 * @ModificationHistory
 */
public class CommentService {

	private static List<Comment> getCommentsMethod(String soapAction, List<BasicNameValuePair> params) throws Exception {
		SoapObject resultObject = null;
		try {
			resultObject = SoapService.getSoapObject(soapAction, params);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return parseData(resultObject);
	}

	private static boolean addCommentMethod(String soapAction, List<BasicNameValuePair> params) throws Exception {
		SoapObject resultObject = null;
		resultObject = SoapService.getSoapObject(soapAction, params);
		return true;
	}

	private static List<Comment> parseData(SoapObject resultObject) throws Exception {
		List<Comment> list = new ArrayList<Comment>();

		for (int i = 0; i < resultObject.getPropertyCount(); i++) {
			SoapObject childObject = (SoapObject) resultObject.getProperty(i);
			// Log.d("childObject", "" + childObject.toString());
			Comment comment = new Comment();
			comment.setComment(childObject.getPropertyAsString("comment"));
			comment.setId(Integer.parseInt(childObject.getPropertyAsString("id")));
			comment.setScore(Double.parseDouble(childObject.getPropertyAsString("score")));
			comment.setUserName(childObject.getPropertyAsString("userName"));
			comment.setTime(childObject.getPropertyAsString("submitTime"));
			// comment.setStore(store);
			list.add(comment);
		}
		return list;
	}

	/**
	 * 根据店铺id获取该店铺所拥有的评论个数
	 * 
	 * @param id
	 *            店铺id
	 * @return 评论总的数目
	 * @throws Exception
	 */
	public static int getCommentsCountByStoreId(int id) throws Exception {
		String soapAction = "getCommentsCountByStoreId";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", "" + id));
		SoapObject resultObject = SoapService.getSoapObject(soapAction, params);
		return Integer.parseInt(resultObject.getProperty(0).toString());
	}

	/**
	 * 根据店铺id,查找一定数量的评论
	 * 
	 * @param id
	 *            店铺id
	 * @param first
	 *            起始位置
	 * @param count
	 *            查找个数
	 * @return
	 * @throws Exception
	 */
	public static List<Comment> getCommentsByStoreId(int id, int first, int count) throws Exception {
		String soapAction = "getCommentsByStoreId";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", "" + id));
		params.add(new BasicNameValuePair("firstResult", "" + first));
		params.add(new BasicNameValuePair("maxResults", "" + count));
		return getCommentsMethod(soapAction, params);
	}

	/**
	 * 根据商店id查找拥有的评论。
	 * 
	 * @param id
	 *            查找的商店id
	 * @return
	 * @throws Exception
	 */
	public static List<Comment> getCommentsByStoreId(int id) throws Exception {
		String soapAction = "getComments";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("id", "" + id));
		return getCommentsMethod(soapAction, params);
	}

	/**
	 * 添加评论
	 * 
	 * @param storeId
	 *            店铺id
	 * @param comment
	 *            评论内容
	 * @param score
	 *            评分
	 * @param userName
	 *            用户名
	 * @param submitTime
	 *            评论时间
	 * @return
	 * @throws Exception
	 */
	public static boolean addComment(int storeId, String comment, float score, String userName, String submitTime) throws Exception {
		String soapAction = "addComment";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", storeId + ""));
		params.add(new BasicNameValuePair("comment", comment));
		params.add(new BasicNameValuePair("score", score + ""));
		params.add(new BasicNameValuePair("userName", userName));
		params.add(new BasicNameValuePair("submitTime", submitTime));

		return addCommentMethod(soapAction, params);
	}

	/**
	 * 添加评论
	 * 
	 * @param comment
	 *            需要添加的评论
	 * @return 添加评论是否成功
	 */
	public static boolean addComment(Comment comment) throws Exception {
		String soapAction = "addComment";
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("storeId", comment.getStoreId() + ""));
		params.add(new BasicNameValuePair("comment", comment.getComment()));
		params.add(new BasicNameValuePair("score", comment.getScore() + ""));
		params.add(new BasicNameValuePair("userName", comment.getUserName()));
		params.add(new BasicNameValuePair("submitTime", comment.getTime()));

		return addCommentMethod(soapAction, params);
	}
}
